package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.domain.TravelContent;
import cn.wolfcode.luowowo.article.mapper.TravelContentMapper;
import cn.wolfcode.luowowo.article.mapper.TravelMapper;
import cn.wolfcode.luowowo.article.query.TravelsQuery;
import cn.wolfcode.luowowo.article.service.ITravlesService;
import cn.wolfcode.luowowo.article.vo.TravelPersistenceVo;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcoe.luowowo.common.exception.LogicException;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class TravlesServiceImpl implements ITravlesService {
    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private TravelContentMapper travelContentMapper;
    @Override
    public PageInfo query(TravelsQuery qo) {
        PageHelper.startPage( qo.getCurrentPage(), qo.getPageSize(), qo.getOrder() );

        return new PageInfo( travelMapper.selectForList( qo ) );
    }

    @Override
    public Long saveOrUpdate(Travel travel,UserInfo userInfo) {

        String content = travel.getTravelContent().getContent();
        if (content.length() > 200) {
            travel.setSummary( content.substring( 0, 200 ) );
        } else {
            travel.setSummary( content );
        }

        if (travel.getId() == null) {
            travel.setCreateTime( new Date() );
            travelMapper.insert( travel );
            travel.getTravelContent().setId( travel.getId() );
            travelContentMapper.insert( travel.getTravelContent() );

        } else {
            //先判读更改的id,是不是当前的用户,如果不是的话,会抛异常
            this.checkAndQueryTravel( travel.getId(),userInfo );
            travelMapper.updateByPrimaryKey( travel );
            travelContentMapper.update( travel.getTravelContent() );

        }

        return travel.getId();


    }

    @Override
    public Travel get(Long id) {
        Travel travel = travelMapper.selectByPrimaryKey( id );
        return travel;
    }

    @Override
    public TravelContent queryContent(Long id) {
        TravelContent content = travelContentMapper.selectByPrimaryKey( id );
        return content;
    }

    @Override
    public List <Travel> selectViewNumTop3(Long destId) {
        List <Travel> travels = travelMapper.selectViewNumTop3( destId );
        return travels;

    }

    @Override
    public void updateState(Long id, int state) {
        if (state == 2) {
            travelMapper.updateState( id, state, new Date() );
        } else {
            travelMapper.updateState( id, state, null );
        }
    }
    @Override
    public Travel checkAndQueryTravel(Long travelId, UserInfo userInfo) {
        Travel travel = travelMapper.selectByPrimaryKey( travelId );
        if (travel.getAuthor().getId() != userInfo.getId()) {
            throw new LogicException( "非法操作" );
        }
        return travel;
    }

    @Override
    public List <Travel> list() {

        List <Travel>  list=   travelMapper.list();
        return list;
    }

    @Override
    public void updateStatisVo(TravelPersistenceVo spvo) {
        travelMapper.updateStatisVo(spvo);
    }

    @Override
    public List<Travel> queryTravelByTop5(Long sid) {
        return travelMapper.selectTravelByTop5(sid);
    }



    @Override
    public List<Travel> queryFroList(Long sid) {
        return travelMapper.queryFroList(sid);
    }

    @Override
    public int queryForTotal(Long sid) {
        return travelMapper.selectForTotal(sid);
    }

    @Override
    public Travel queryforTravelByDestId(long destId) {

        return travelMapper.queryforTravelByDestId(destId);
    }

    @Override
    public int queryForReplynum(Long uid) {
        return travelMapper.selectForReplynum(uid);
    }

    @Override
    public int queryForViewnum(Long uid) {
        return travelMapper.selectForViewnum(uid);
    }

    @Override
    public List<Travel> queryTravel(List<Long> travelsIds) {
        return travelMapper.selectTravel(travelsIds);
    }
}
